home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / vidhandl / fontedit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-03  |  9.4 KB  |  421 lines

  1. /* FONTEDIT - Edits DOS character font files (.FNT)                 */
  2. /* Freeware version                                                 */
  3. /* By Marcio Afonso Arimura Fialho                                  */
  4. /* http://pessoal.iconet.com.br/jlfialho                            */
  5. /* e-mail: jlfialho@iconet.com.br or (alternate) jlfialho@yahoo.com */
  6.  
  7. //To select edited files, use FONTSEL
  8.  
  9. #include <stdio.h>
  10. #include <crt.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <conio.h>
  14. #include <setjmp.h>
  15. #include <dos.h>
  16.  
  17. #define EGA 3 //EGA color adapters
  18. #define VGA 9 //VGA/SVGA+ color adapters
  19.  
  20. #define intm(intnum) asm int intnum
  21.  
  22. typedef unsigned char byte;
  23. typedef unsigned int  word;
  24. typedef unsigned long dword;
  25.  
  26. //Disables CTRL-C
  27.  
  28. jmp_buf ctrl_c;
  29.  
  30. int c_break(void)
  31.  {
  32.     longjmp (ctrl_c,1);
  33.     return 1;
  34.  }
  35.  
  36. //Mouse routines:
  37.  
  38. int  mouserst (int *nbuttons); //resets and checks for mouse
  39. void mouseon (void); //turns on mouse pointer
  40. void mouseread(int *collum, int *line, int *buttom); //reads mouse position and button
  41. void mousewrite (int collum, int line); //changes mouse pointer position
  42. void mouseoff(void); //switch off mouse pointer
  43.  
  44. //Miscellaneous routines:
  45. int  getchx(); //enhaced version of getch, reads extended characters
  46. void readfname (char *target, char *source, char *ext);
  47.     //reads filename and appends extension (ext) if none is given.
  48.  
  49. //Global variables
  50. byte *buffer;
  51. byte *aux,*aux2;
  52. byte schar=0;
  53. int c_height;
  54. FILE *source;
  55. byte bufchar[32]; //used by cut and paste tool
  56. int prog_flags=0; //bit0 => if set redraws miniature
  57.  
  58. #define target source  //the source file is also the target file
  59.  
  60. //Specific program routines
  61. void redraw();
  62. void redrawmin();
  63. void filesave(char *name);
  64. void copychr();
  65. void pastechr();
  66. void invertchr();
  67. void cleanchr();
  68. void movepos(int dir);
  69.  
  70.  
  71. #define E_XI 10
  72. #define E_YI 5
  73. #define M_XI 60
  74. #define M_YI 10
  75.  
  76. void main (int n, char *ent[3])
  77.   {
  78.     int c0,c1;
  79.     int a0,a1,a2;
  80.     int inpkey, mouseb, mousex, mousey;
  81.     int mouseb2,mousex2, mousey2;
  82.     char *path;
  83.     char *msg_about="\nType FONTEDIT with no parameters to obtain help\n";
  84.     char licos[128];
  85.     int cont;
  86.  
  87.     //Disables CTRL-C and assings for it a new function
  88.     ctrlbrk(c_break);
  89.     if(setjmp(ctrl_c))
  90.      {
  91.         crt_gotoxy (0,0);
  92.         fillbar ('░',0,0,2,2,0x19);
  93.         copychr ();
  94.         goto pula2;
  95.      }
  96.  
  97.     if (n<2)
  98.      {
  99.         printf ("FONTEDIT ver 1.1 - EDITS DOS CHARACTER FONT FILES (.FNT)\n\n\
  100. Usage:   FONTEDIT <font name[.FNT]> [height]\n\n\
  101. \theight is the character height in pixels (default = 16)\n\n\
  102. By Marcio Afonso Arimura Fialho\nhttp://pessoal.iconet.com.br\n\
  103. e-mail: jlfialho@iconet.com.br\n");
  104.         return;
  105.      }
  106.  
  107.     if (n>=3) //reads the character height
  108.      {
  109.         c_height=atoi(ent[2]);
  110.         if((unsigned)c_height>32u)
  111.          {
  112.             printf ("ERROR: Incorrect input parameters.%s",msg_about);
  113.             return;
  114.          }
  115.      }
  116.      else
  117.         c_height=16;
  118.  
  119.     readfname (licos,ent[1],".FNT");//appends the .FNT extension to licos
  120.       //if none is given
  121.  
  122.     source=fopen (licos,"rb");
  123.     if(source==NULL)
  124.      {
  125.         printf ("ERROR: File \"%s\" doesn't exist or couldnt be read.%s",licos,msg_about);
  126.         fcloseall();
  127.         return;
  128.      }
  129.  
  130.     buffer=(byte *)malloc(256*c_height); //allocates memory for character font
  131.  
  132.     if (buffer==NULL) //if not possible to allocate memory
  133.      {     //displays error message and returns
  134.         printf ("ERROR: Not enough memory to store font.%s",msg_about);
  135.         fcloseall ();
  136.         return;
  137.      }
  138.     for (aux=buffer,cont=0;cont<c_height*256;cont++) //loads character font into buffer
  139.      {
  140.         *aux=(byte)fgetc (source);
  141.         aux++;
  142.      }
  143.     fclose (source);
  144.  
  145.  //Set mode and draws the background
  146.     setcrtmode (3);
  147.     textmode (64);
  148.     crt_init (VGA); //if your monitor is EGA, replace crt_init argument
  149.     fillscr ('░',0x19);
  150.  
  151.   //Display messages on screen
  152.     printsj ("* * * FONTEDIT ver 1.1 * * *",0,0x1e);
  153.     printsj ("DOS character font editor for EGA/VGA/SVGA+ videos",2,0x17);
  154.  
  155.     printx ("ESC\06\27 - Exit \06\37F2\06\27 - Saves \06\37  PG UP/DN and CTRL PG UP/DN\06\27 - Selects Character\n\
  156. \t\06\37Ctrl C/Ins\06\27 - Copies character    \06\37Ctrl V\06\27 - Paste character\n\
  157. \t\06\37Alt I \06\27 - Inverts caractere    \06\37ALT-C\06\27 - Clears character\n\
  158. \t\06\37Ctrl \06\36(arrow for up, down, sides)\06\27 - Moves character pattern\n\
  159. \t\06\37Alt R \06\27(Switch On/Off miniature display",5,44,0x1f);
  160.  
  161.     moldurad (E_XI,E_YI,E_XI+9,E_YI+1+c_height,0x1f);
  162.     printc ('1',E_XI+2,E_YI,0x1f);
  163.     redraw();
  164.  
  165.     mouseon ();
  166.     do
  167.      {
  168.         if (kbhit()) //if is there a keystroke available and the control is not
  169.             inpkey=getchx (); //exclusive of mouse
  170.          else
  171.             inpkey=0;
  172.  
  173.         mouseb2=mouseb;
  174.         mousex2=mousex;
  175.         mousey2=mousey;
  176.         switch (inpkey)
  177.          {
  178.             case 0x1b: goto fim;
  179.             case 0x3c00: filesave(licos); break;
  180.             case 0x4800: mousey--; goto pula1;
  181.             case 0x5000: mousey++; goto pula1;
  182.             case 0x4b00: mousex--; goto pula1;
  183.             case 0x4d00: mousex++; goto pula1;
  184.  
  185.             case 0x1300: prog_flags^=0x01; redraw(); goto pula2;
  186.             case 0x4900: schar++; goto pula2; //PG UP
  187.             case 0x5100: schar--; goto pula2; //PG DN
  188.             case 0x8400: schar+=16; goto pula2; //CTRL PG UP
  189.             case 0x7600: schar-=16; goto pula2; //CTRL PG DN
  190.             case 0x9200: copychr(); goto pula2; //CTRL INS
  191.             case 0x16  : pastechr(); goto pula2; //CTRL-V
  192.             case 0x1700: invertchr(); goto pula2; //ALT-I
  193.             case 0x2e00: cleanchr(); goto pula2;  //ALT-C
  194.             case 0x8d00: movepos(2); goto pula2;  //CTRL up
  195.             case 0x7400: movepos(0); goto pula2;  //CTRL right
  196.             case 0x9100: movepos(3); goto pula2;  //CTRL down
  197.             case 0x7300: movepos(1); goto pula2;  //CTRL left
  198.  
  199.             pula1: mousewrite (mousex*8, mousey*8); break;
  200.             pula2: redraw();
  201.          }
  202.         mouseread (&mousex,&mousey,&mouseb);
  203.         mousex=mousex/8;
  204.         mousey=mousey/8;
  205.         a0=0;
  206.         if ( (!mouseb2 && mouseb) || inpkey=='+' || inpkey=='-' || inpkey==0x0d || (mouseb && (mousex!=mousex2 || mousey!=mousey2)))
  207.             a0++; //indicates that is to change something
  208.         if (a0 && mousex>E_XI && mousex<(E_XI+9) && mousey>E_YI && mousey<=(E_YI+c_height))
  209.          {
  210.             a0=mousey-E_YI-1;
  211.             a1=E_XI+8-mousex;
  212.             a2=1;
  213.             for(c0=0;c0<a1;c0++,a2*=2);
  214.             aux=buffer + schar * c_height + a0;
  215.             if (inpkey=='+' || mouseb==0x01)
  216.                 (*aux)|=a2;
  217.             if (inpkey=='-' || mouseb==0x02)
  218.                 (*aux)&=~a2;
  219.             if (inpkey==0x0d || mouseb==0x03)
  220.                 (*aux)^=a2;
  221.             redraw();
  222.          }
  223.      }
  224.     while (1);
  225.  
  226.   fim: //end of the program
  227.     mouseoff ();
  228.     setcrtmode (3);
  229.     fcloseall();
  230.  }
  231.  
  232. void redraw()
  233.  {
  234.     int a0,a2;
  235.     int c0,c1,c2;
  236.     mouseoff ();
  237.     for (c0=0;c0<c_height;c0++)
  238.         for (c1=0;c1<8;c1++)
  239.          {
  240.             a2=1;
  241.             for(c2=0;c2<c1;c2++,a2*=2); //a2=2^c1
  242.             a0=(*(buffer + schar * c_height + c0))&a2;
  243.             if (a0)
  244.                 printc('█',E_XI+8-c1,E_YI+1+c0,0x1f);
  245.             else
  246.                 printc ('·',E_XI+8-c1,E_YI+1+c0,0x1f);
  247.          }
  248.     printsf (50,5,0x1f,"1 - Character => %02Xh = %03d",schar,schar);
  249.     if (prog_flags&0x01)
  250.         redrawmin();
  251.      else
  252.         fillbar ('░',M_XI-2,M_YI-2,M_XI+7,M_YI+4+(c_height-1)/8,0x19);
  253.     mouseon ();
  254.  }
  255.  
  256. void redrawmin()
  257.  {
  258.     int c0;
  259.     int a0;
  260.     char chrdisp[32];
  261.     a0=(c_height-1)/8;
  262.     prints ("Miniature:",M_XI-2,M_YI-2,0x1f);
  263.     moldurad (M_XI,M_YI,M_XI+4,M_YI+4+a0,0x1f);
  264.     fillbar (' ',M_XI+1,M_YI+1,M_XI+3,M_YI+3+a0,0x1f);
  265.  
  266.     for (c0=0;c0<c_height;c0++)
  267.         chrdisp[c0]=*(buffer+schar*c_height+c0);
  268.     for (;c0<32;c0++)
  269.         chrdisp[c0]=0;
  270.     changechar_height=8;
  271.     changechar (chrdisp,0xe0,4);
  272.     for (c0=0;c0<=a0;c0++)
  273.         printc (0xe0+c0,M_XI+2,M_YI+2+c0,0x1f);
  274.  }
  275.  
  276. void filesave(char *name)
  277.  {
  278.     int c0;
  279.     target=fopen (name,"wb");
  280.     if(target==NULL)
  281.      {
  282.         prints ("ERROR: File can't be open for writing.",15,30,0x9e);
  283.         putchar (0x07);
  284.         getch ();
  285.         fillbar ('░',0,30,79,30,0x19);
  286.         fcloseall();
  287.         return;
  288.      }
  289.     aux=buffer;
  290.     for (c0=0;c0<c_height*256;c0++,aux++)
  291.         fputc(*aux,target);
  292.     fseek (target,-1,SEEK_END);
  293.     fclose(target);
  294.  }
  295.  
  296. void copychr()
  297.  {
  298.     int c0;
  299.     aux=buffer + schar * c_height;
  300.     aux2=bufchar;
  301.     for (c0=0;c0<c_height;c0++,aux2++,aux++)
  302.         *aux2=*aux;
  303.  }
  304.  
  305. void pastechr()
  306.  {
  307.     int c0;
  308.     aux=buffer + schar * c_height;
  309.     aux2=bufchar;
  310.     for (c0=0;c0<c_height;c0++,aux2++,aux++)
  311.         *aux=*aux2;
  312.  }
  313.  
  314. void invertchr()
  315.  {
  316.     int c0;
  317.     aux=buffer + schar * c_height;
  318.     for (c0=0;c0<c_height;c0++,aux++)
  319.         *aux=~(*aux);
  320.  }
  321.  
  322. void cleanchr()
  323.  {
  324.     int c0;
  325.     aux=buffer + schar * c_height;
  326.     for (c0=0;c0<c_height;c0++,aux++)
  327.         *aux=0;
  328.  }
  329.  
  330. void movepos(int dir)
  331.  {
  332.     int c0;
  333.     aux=buffer + schar * c_height;
  334.     if (dir<4)
  335.         for (c0=0;c0<c_height;c0++,aux++)
  336.          {
  337.             if (dir==0)    //moves to the right
  338.                 *aux=(*aux)/2;
  339.             else if (dir==1) //moves to the left
  340.                 *aux=(*aux)*2;
  341.             else if (dir==2) //moves to up
  342.              {
  343.                 if (c0!=(c_height-1))
  344.                     *aux=*(aux+1);
  345.                 else
  346.                     *aux=0;
  347.              }
  348.           }
  349.     if(dir==3) //moves to down
  350.      {
  351.         aux--;
  352.         for (c0=c_height-1;c0>=0;c0--,aux--)
  353.          {
  354.             if (c0)
  355.                 *aux=*(aux-1);
  356.             else
  357.                 *aux=0;
  358.          }
  359.      }
  360.  }
  361.  
  362. int mouserst (int *nbuttons)
  363.  {
  364.     int i,j;
  365.     _AX=0;
  366.     intm(0x33);
  367.     i=_AX;
  368.     j=_BX;
  369.     *nbuttons=j;
  370.     return i;
  371.  }
  372.  
  373. void mouseon (void)
  374.  {
  375.     _AX=1;
  376.     intm(0x33);
  377.  }
  378.  
  379. void mouseread(int *collum, int *line, int *buttom)
  380.  {
  381.     int i,j,k;
  382.     _AX=3;
  383.     intm (0x33);
  384.     i=_CX;
  385.     j=_DX;
  386.     k=_BX;
  387.     *collum=i;
  388.     *line=j;
  389.     *buttom=k;
  390.  }
  391.  
  392. void mousewrite (int collum, int line)
  393.  {
  394.     _CX=collum;
  395.     _DX=line;
  396.     _AX=4;
  397.     intm (0x33);
  398.  }
  399.  
  400. void mouseoff(void)
  401.  {
  402.     _AX=2;
  403.     intm (0x33);
  404.  }
  405.  
  406. int getchx()
  407.  {
  408.     int i;
  409.     _AH=0x07;
  410.     intm(0x21);
  411.     i=_AL;
  412.     if (i==0)
  413.      {
  414.         _AH=0x07;
  415.         intm(0x21);
  416.         i=_AL*0x100;
  417.      }
  418.     return i;
  419.  }
  420.  
  421. #include "readname.cpp"